﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Visa.DB;

namespace Visa.Service
{
    public class RolesSer
    {
        private VisaEntities db = new VisaEntities();

        public void DeleteRoles(Role category)
        {
            try
            {
                var CategotyToDetach = db.Roles.SingleOrDefault(o => o.RoleID == category.RoleID);
                CategotyToDetach.Deleted = true;
                db.SaveChanges();
            }
            catch { }
        }

        public void AddUserInrolesID(int userId, int RoledID)
        {
            try
            {
                UserInRole UIR = new UserInRole();
                UIR.RolesID = RoledID;
                UIR.UserID = userId;
                db.UserInRoles.Add(UIR);
                db.SaveChanges();
            }
            catch { }
        }

        public bool checkExitsRolesiD(int userId, int RoledID)
        {
            try
            {
                var c = db.UserInRoles.SingleOrDefault(k => k.UserID == userId && k.RolesID == RoledID);
                if (c != null) return true;
                else return false;
            }
            catch { return false; }
        }

        public void removeRolesInUsser(int userId, int RoledID)
        {
            try
            {
                var c = db.UserInRoles.SingleOrDefault(k => k.UserID == userId && k.RolesID == RoledID);
                db.UserInRoles.Remove(c);
                db.SaveChanges();
            }
            catch { }
        }

        public List<Role> GetAllRoles(bool showHidden = false)
        {
            try
            {
                if (showHidden == true)
                {
                    var c = from i in db.Roles
                            where i.Deleted == false
                            select i;
                    return c.ToList();
                }
                else
                {
                    var c = from i in db.Roles
                            where i.Deleted == false && i.Staus == true
                            orderby i.DateCreate descending
                            select i;
                    return c.ToList();
                }

            }

            catch { return null; }
        }

        public Role GetRolesById(int categoryId)
        {
            try
            {
                if (categoryId == -1)
                    return null;

                return db.Roles.SingleOrDefault(l => l.RoleID == categoryId);
            }
            catch { return null; }
        }

        public void InsertRoles(Role category)
        {
            db.Roles.Add(category);
            db.SaveChanges();
        }

        public void UpdateRoles(Role category)
        {
            var CategotyToDetach = db.Roles.SingleOrDefault(o => o.RoleID == category.RoleID);
            CategotyToDetach.Name = category.Name;
            CategotyToDetach.Staus = category.Staus;
            db.SaveChanges();
        }

        public List<Role> SearchCategory(string name)
        {
            try
            {
                var c = from i in db.Roles
                        where i.Deleted == false
                        && i.Name.Contains(name)
                        select i;
                return c.ToList();
            }
            catch { return null; }
        }

        public Role GetRolerInRoleName(string RolesName)
        {
            try
            {
                return db.Roles.SingleOrDefault(l => l.Name == RolesName);
            }
            catch { return null; }
        }

        public void AddUsserInRoles(int UserID, string RolesName)
        {
            try
            {
                var c = db.Roles.SingleOrDefault(l => l.Name == RolesName);
                UserInRole UIR = new UserInRole();
                UIR.RolesID = c.RoleID;
                UIR.UserID = UserID;
                db.UserInRoles.Add(UIR);
                db.SaveChanges();
            }
            catch { }

        }

        public string[] GetRolesForUser(string userName)
        {
            var c = (from i in db.Roles
                     join m in db.UserInRoles on i.RoleID equals m.RolesID
                     join l in db.Users on m.UserID equals l.UsID
                     where l.UserName == userName
                     select i).ToList();

            List<string> Temp = new List<string>();

            if (c.Count() > 0)
            {
                foreach (var g in c)
                {
                    Temp.Add(g.Name);
                }
            }

            return Temp.ToArray();
        }

        public bool checkUserIRoles(int UserID, string RolesName)
        {
            try
            {
                var cRoles = db.Roles.SingleOrDefault(l => l.Name == RolesName);
                var c = db.UserInRoles.SingleOrDefault(l => l.UserID == UserID && l.RolesID == cRoles.RoleID);
                if (c != null) return true;
                else return false;

            }
            catch { return false; }

        }

        public bool IsUserInRole(string username, string roleName)
        {
            try
            {
                var c = (from i in db.Roles
                         join m in db.UserInRoles on i.RoleID equals m.RolesID
                         join l in db.Users on m.UserID equals l.UsID
                         where l.UserName == username && i.Name == roleName
                         select i).SingleOrDefault();
                if (c != null) return true;
                else return false;

            }
            catch { return false; }

        }

        public bool CheckUSerISAdmin(string UserName)
        {
            try
            {
                var c = (from i in db.Roles
                         join m in db.UserInRoles on i.RoleID equals m.RolesID
                         join l in db.Users on m.UserID equals l.UsID
                         where l.UserName == UserName
                         select i).SingleOrDefault();
                if (c != null) return true;
                else return false;
            }
            catch { return false; }
        }

        public bool GetUserIsRolesisAmin(bool isadmin, int UserID)
        {
            try
            {
                var c = (from i in db.Roles
                         join m in db.UserInRoles on i.RoleID equals m.RolesID
                         join l in db.Users on m.UserID equals l.UsID
                         where l.UsID == UserID
                         select i).SingleOrDefault();
                if (c != null) return true;
                else return false;
            }
            catch { return false; }
        }

        public bool GetUserIsRolesisNews(bool isNews, int UserID)
        {
            try
            {
                var c = (from i in db.Roles
                         join m in db.UserInRoles on i.RoleID equals m.RolesID
                         join l in db.Users on m.UserID equals l.UsID
                         where l.UsID == UserID
                         select i).SingleOrDefault();
                if (c != null) return true;
                else return false;
            }
            catch { return false; }
        }

        public bool GetUserIsRolesisSystem(bool system, int UserID)
        {
            try
            {
                var c = (from i in db.Roles
                         join m in db.UserInRoles on i.RoleID equals m.RolesID
                         join l in db.Users on m.UserID equals l.UsID
                         where l.UsID == UserID
                         select i).SingleOrDefault();
                if (c != null) return true;
                else return false;
            }
            catch { return false; }
        }

    }
}
